package com.zhu.ttwords.view.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhu.ttwords.R;
import com.zhu.ttwords.bean.WordBean;

/**
 * Created by KnightYe on 2015/4/4.
 */
public class ExamView extends FrameLayout {
    //单词预览一共有一下几种状态
    public static final int STATUS_INIT = 0x00;//初始化时候，exam不显示，context显示，prono显示，mark不显示，exam可编辑
    public static final int STATUS_ATTATION = 0x10;//学习答题时，exam不显示，context显示，prono显示，mark显示，exam可编辑
    public static final int STATUS_WRONG = 0x20;//学习答题结束时，exam不显示，context显示，prono显示，mark显示，exam可编辑
    public static final int STATUS_RIGHT = 0x30;//学习答题结束时，exam不显示，context显示，prono显示，mark显示，exam可编辑

    private boolean isStudayMode = true;// studay is true,exam is false;
    private int status = 0;//状态标志位。
    private int alph = 0;
    WordBean bean;//存储数据的bean。
    ViewGroup group;//content和prono的集合。
    TextView explain;
    TextView pos;
    TextView prono;
    EditText exam;
    EditText content;
    ImageView mark;
    ViewGroup focus;
    OnFocusChangeListener focusChangeListener;
    TextView.OnEditorActionListener actionListener;

    public ExamView(final Context context) {
        super(context);
        bean = new WordBean();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rootView = inflater.inflate(R.layout.view_study, this);
        group = (ViewGroup) rootView.findViewById(R.id.view_study_down);
        explain = (TextView) rootView.findViewById(R.id.view_study_explain);
        pos = (TextView) rootView.findViewById(R.id.view_study_pos);
        prono = (TextView) rootView.findViewById(R.id.view_study_prono);
        exam = (EditText) rootView.findViewById(R.id.view_study_test);
        content = (EditText) rootView.findViewById(R.id.view_study_content);
        mark = (ImageView) rootView.findViewById(R.id.view_study_mark);
        focus = (ViewGroup) rootView.findViewById(R.id.view_study_focus);
        actionListener = new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch (actionId) {
                    case EditorInfo.IME_ACTION_NEXT:
                        checkWord(isStudayMode?content.getText():exam.getText());
                        break;
                }
                return false;
            }
        };
        exam.setOnEditorActionListener(actionListener);
        focusChangeListener = new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                switch (v.getId()) {
                    case R.id.view_study_content:
                    case R.id.view_study_test:
                        if (hasFocus) {
                            if (status == STATUS_RIGHT ) {
                                if (isStudayMode) {
                                    isStudayMode = false;
                                    exam.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            exam.requestFocus(0);
                                        }
                                    }, 500);
                                }else{
                                    exam.clearFocus();
                                    return;
                                }
                            }
                            setStatus(STATUS_ATTATION);
                        }
                        break;
                    default:
                        break;
                }
            }
        };
        exam.setOnFocusChangeListener(focusChangeListener);
        setStatus(status);
    }

    public ExamView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExamView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ExamView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public WordBean getBean() {
        return bean;
    }

    public void setBean(WordBean bean) {
        this.bean = bean;
        prono.setText(bean.getProno());
        explain.setText(bean.getExplain());

        pos.setText(bean.getPos());
        content.setText(bean.getContent());
        content.setHint(bean.getContent());
        exam.setHint(bean.getContent());
    }

    public void setAlph(int alph) {
        this.alph = alph;
    }

    public void setStatus(int status) {
        switch (status) {
            case STATUS_INIT:
                group.setVisibility(View.INVISIBLE);
                exam.setVisibility(View.VISIBLE);
                exam.getEditableText().clear();
                mark.setVisibility(View.INVISIBLE);
                break;
            case STATUS_ATTATION:
                group.setVisibility(View.INVISIBLE);
                exam.getEditableText().clear();
                mark.setVisibility(View.VISIBLE);
                mark.setImageResource(R.mipmap.img_mark_attention);
                break;
            case STATUS_RIGHT:
                mark.setVisibility(View.VISIBLE);
                mark.setImageResource(R.mipmap.img_mark_right);
                break;
            case STATUS_WRONG:
                mark.setVisibility(View.VISIBLE);
                mark.setImageResource(R.mipmap.img_mark_wrong);
                break;
            default:
                break;
        }
        this.status = status;
    }

    private void checkWord(CharSequence a) {
        if (a.toString().equals(bean.getContent())) {
            setStatus(STATUS_RIGHT);
        } else {
            setStatus(STATUS_WRONG);
        }
    }
}
